//
//  GuideViewController.m
//  忘不了
//
//  Created by rimi on 16/7/7.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "GuideViewController.h"
#import "SDAutoLayout.h"
#import <AVOSCloud/AVOSCloud.h>

@interface GuideViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@end

@implementation GuideViewController

- (void)viewDidLoad {
    AVObject *post = [AVObject objectWithClassName:@"hanhanhan"];
    [post setObject:@"Hello World!" forKey:@"words"];
    [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // 保存成功了！
            NSLog(@"保存成功");
        }
    }];
    
    [super viewDidLoad];
    [self inializeUserInterface];
}
- (void)inializeUserInterface{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds) * 3, CGRectGetHeight(self.view.bounds));
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    NSMutableArray *imageArr = @[].mutableCopy;
    
    for (NSInteger index = 0; index < 3; index ++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",index + 1]];
        [imageArr addObject:image];
    }
    for (NSInteger index = 0; index < 3; index ++) {
        UIImageView *image = [[UIImageView alloc]initWithImage:imageArr[index]];
        image.frame = CGRectMake(index * CGRectGetWidth(self.view.bounds), 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
        [self.scrollView addSubview:image];
    }
    
    //分页控制器
    _pageControl = [[UIPageControl alloc]init];
    _pageControl.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 30);
    _pageControl.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) * 0.96);
    //总页数
    _pageControl.numberOfPages = 3;
    //当前页
    _pageControl.currentPage = 0;
    //当前指示器的颜色
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    [_pageControl addTarget:self action:@selector(showIndicatorPage:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_pageControl];
    
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.backgroundColor = [UIColor colorWithRed:0.0588 green:0.7255 blue:0.6706 alpha:1.0];
    loginButton.layer.cornerRadius = 10;
    loginButton.layer.masksToBounds = YES;
    [loginButton setTitle:@"立即体验" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor colorWithRed:0.8275 green:0.7961 blue:0.7216 alpha:1.0] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(toLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:loginButton];
    loginButton.sd_layout
    .centerXIs(self.view.bounds.size.width*2+self.view.bounds.size.width/2)
    .bottomSpaceToView(self.scrollView,self.view.bounds.size.width*0.2)
    .heightIs(self.view.bounds.size.width*0.1)
    .widthIs(self.view.bounds.size.width*0.25);
    
}

- (void)toLogin{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.alpha = 0.1;
    } completion:^(BOOL finished) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"F"];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

-(void)showIndicatorPage:(UIPageControl *)sender
{
    [_scrollView setContentOffset:CGPointMake(self.view.bounds.size.width * sender.currentPage, 0) animated:YES];
    
    
    
}

#pragma mark--UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == self.view.bounds.size.width * 2) {
        scrollView.bounces = NO;
    }else{
        scrollView.scrollEnabled = YES;
    }
    NSInteger currentPage = round(scrollView.contentOffset.x/self.view.bounds.size.width);//四舍五入
    _pageControl.currentPage = currentPage;
    
    
    //ceil(double),如果参数是小数，则求最大整数
    //floor(double),如果参数是小数，则求最小整数
    
}
@end
